const express = require("express");
const api = require("../modules/api.js");
const router = express.Router();

// 首页
router.get("/", (req, res, next)=>{
	Promise.all([
		api.home_banner.bind(req)(),
		api.product_query.bind(req)({pageNo: 1, pageSize: 3, isTPOnly: true}),
		api.product_query.bind(req)({pageNo: 1, pageSize: 3, isTPOnly: false})
	]).then(data=>{
		let banners = data[0].result.all;
		console.log(banners);
		let tp_list = data[1].result.list || [];
		let pf_list = data[2].result.list || [];
		res.render("h5/index.html", {tp_list, pf_list, req, banners});
	});
});

// 贴牌
router.get("/tp", (req, res, next)=>{
	Promise.all([
		api.product_query.bind(req)({pageNo: 1, pageSize: 10, isTPOnly: true})
	]).then(data=>{
		let list = data[0].result.list || [];
		res.render("h5/product_list.html", {title: "贴牌定制", list, req});
	});
});

// 批发
router.get("/pf", (req, res, next)=>{
	Promise.all([
		api.product_query.bind(req)({pageNo: 1, pageSize: 10, isTPOnly: false})
	]).then(data=>{
		let list = data[0].result.list || [];
		res.render("h5/product_list.html", {title: "批发代理", list, req});
	});
});

// 商品详情
router.get("/product/:id", (req, res, next)=>{
	let productId = req.params.id;
	Promise.all([
		api.product_detail.bind(req)(productId),
		api.product_rateList.bind(req)({productId, pageNo: 1, pageSize: 1})
	]).then(data=>{
		let product = data[0].result;
		let params = [];
        try{
            params = JSON.parse(product.params);
        }catch(e){
            console.log(e);
        };
        let descs = [];
        let lines = Math.ceil(params.length / 3);
        console.log(lines);

        for(let i = 0; i < lines; i ++){
            let arr = [];
            for(let j = 0; j < 3; j ++){
                arr.push(params[ i * 3 + j]);
            }
            descs.push(arr);
        };
        console.log(descs);
        let rate = (data[1].result.list || [])[0] || {};
		res.render("h5/product_detail.html", { productId , product, descs, rate});
	});
	
});

// 确认下单
router.get("/order_confirm", (req, res, next)=>{
	let order = {};
	console.log("-----order------");
	console.log(order);
	res.render("h5/order_confirm.html", {order});
});

// 订单列表
router.get("/order/list", (req, res, next)=>{
	Promise.all([
		api.order_list.bind(req)({pageNo: 1, pageSize: 10})
	]).then(data=>{
		let list = data[0].result.list || [];
		res.render("h5/order_list.html", {list});
	});
});


// 加工列表
router.get("/process/list", (req, res, next)=>{
	Promise.all([
		api.process_myList.bind(req)({pageNo: 1, pageSize: 10})
	]).then(data=>{
		let process_list = data[0].result.list || [];
		res.render("h5/process_list.html", {process_list});
	});
	
});

// 操作成功页面
router.get("/success", (req, res, next)=>{
	res.render("h5/success.html");
});


//  我的页面
router.get("/personal", (req, res, next)=>{
	let is_logined = false;
	let user = {};
	if(req.session.token && req.session.uid){
		is_logined = true;
		user = req.session.user;
	};
	res.render("h5/personal.html", {req, is_logined, user});
});


// 订单详情
router.get("/order/:orderNo", (req, res, next)=>{
	let orderNo = req.params.orderNo;
	Promise.all([
		api.order_detail.bind(req)(orderNo)
	]).then(data=>{
		let order = data[0].result || {};
		console.log(order);
		res.render("h5/order_detail.html", {order});
	});
});

// 登录页面
router.get("/login", (req, res, next)=>{
	console.log(req.session);
	res.render("h5/login.html");
});

// 关于
router.get("/about", (req, res, next)=>{
	res.render("h5/about.html");
});

// 工厂入驻
router.get("/factory/entry", (req, res, next)=>{
	res.render("h5/factory_entry.html");
});

// 加工定制页面
router.get("/process", (req, res, next)=>{
	res.render("h5/process.html", {req});
});

// 评价
router.get("/rate/:id", (req, res, next)=>{
	let productId = req.params.id;
	Promise.all([
		api.product_rateList.bind(req)({pageNo: 1, pageSize: 10, productId})
	]).then(data=>{
		let list = data[0].result.list || [];
		console.log(list);
		console.log(data[0].result.page);
		res.render("h5/rate_list.html", {list, productId});
	});
	
});

// 选择地址
router.get("/address", (req, res, next)=>{
	Promise.all([
		api.address_myList.bind(req)()
	]).then(data=>{
		console.log(data);
		let list = data[0].result.list || [];
		let id = req.query.id;
		console.log(list);
		res.render("h5/address_list.html", {list, id});
	});
});

module.exports = router;




















